package com.project.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.DiscountApplyBean;
import com.project.bean.DiscountBean;
import com.project.dto.PromotionDto;
import com.project.service.IDiscountApplyService;
import com.project.service.IDiscountService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 促销controller
 */
@RestController
@RequestMapping("discount")
public class DiscountController {
    @Autowired
    private IDiscountService discountService;
    @Autowired
    private IDiscountApplyService discountApplyService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 添加促销商品
     * @param discountObj 促销商品对象
     * @return 添加状态
     */
    @RequestMapping("addDiscountProduct")
    public String addDiscountProduct(DiscountBean discountObj){
        return discountService.addDiscountProduct(discountObj);

    }

    /**
     * 动态分页查询促销商品
     * @param page 页码
     * @return 分页对象
     */
    @RequestMapping("cutDiscountByItem")
    public IPage<DiscountBean> cutDiscountByItem(int page){
        return discountService.cutDiscountByItem(page);
    }



    /**
     * 取消促销
     * @param shopId 商品编号
     */
    @RequestMapping("updatePromotionById")
    public String updatePromotionById(String shopId){
        return discountService.updatePromotionById(shopId, "未促销");
    }

    /**
     * 通过编号修改折扣
     * @param id 促销编号
     * @param discount 折扣
     */
    @RequestMapping("updateDiscountById")
    public String updateDiscountById(long id,double discount){
        return discountService.updateDiscountById(id, discount);
    }

    /**
     * 添加促销申请
     * @param discountApplyObj 促销申请对象
     */
    @RequestMapping("addDiscountApply")
    public String addDiscountApply(DiscountApplyBean discountApplyObj){
        String st=discountApplyService.addDiscountApply(discountApplyObj);
        if (st=="ok") {
            DiscountBean discountBean=discountService.findByShopNumber(discountApplyObj.getShopId());
            PromotionDto promotionDto=new PromotionDto();
            promotionDto.setPromotionRef(discountApplyObj.getDiscountApplyId());
            promotionDto.setPromotionName(discountApplyObj.getName());
            promotionDto.setPromotionDescribe(discountApplyObj.getDescribt());
            promotionDto.setProductRef(discountApplyObj.getShopId());
            promotionDto.setProductName(discountBean.getShopName());
            promotionDto.setProductType(discountBean.getType());
            promotionDto.setProductPrice(discountBean.getOrigina());
            promotionDto.setDiscount(discountBean.getDiscount());
            promotionDto.setNumber(discountBean.getNum());
            String s= JSON.toJSONString(promotionDto);
            rabbitTemplate.convertAndSend("BackPurchaseApply", "Review.key", s);
            return "ok";

        }
        return st;
    }

//    /**
//     * 根据传回的促销申请对象，修改审核状态，如果审核状态为’未通过‘，就修改未通过原因
//     * @param discountApplyBean 促销申请对象
//     * @return 返回修改状态
//     */
//    @RequestMapping("updateStatusByDiscountApplyId")
//    public String updateStatusByDiscountApplyId(DiscountApplyBean discountApplyBean){
//
//        //调用修改审核状态的方法
//        String st=discountApplyService.updateStatusByDiscountApplyId(discountApplyBean.getDiscountApplyId(),discountApplyBean.getStatus());
//        if(st=="ok" &&discountApplyBean.getStatus()=="未通过"){//审核状态修改成功，并且审核状态为’未通过‘就修改未通过原因
//            return discountApplyService.updateReasonByDiscountApplyId(discountApplyBean.getDiscountApplyId(),discountApplyBean.getReason());
//        }
//        return st;
//    }


    /**
     * 查询促销申请表
     * @return 促销申请集合
     */
    @RequestMapping("findDiscountApply")
        public IPage<DiscountApplyBean> findDiscountApply(int pageNO){
        return discountApplyService.findDiscountApply(pageNO);
    }
    /**
     * 通过商品编号查询促销商品
     * @param shopId 商品编号
     * @return 促销商品对象
     */
    @RequestMapping("findByShopNumber")
    public DiscountBean findByShopNumber(String shopId){
        return discountService.findByShopNumber(shopId);
    }
}
